package me.sensen.controller.backend;

import me.sensen.Services;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Sensen
 */

@RestController
public class PrintBackendController {


    @PostMapping(value = "/fileUpload")
    public String upload(@RequestParam("file") MultipartFile file) {
        String path = System.getProperty("user.dir") + "\\files\\" + System.currentTimeMillis() + "\\";
        String filePath = path + file.getOriginalFilename();
        if (file.isEmpty()) {
            return "fileUploadError";
        }
        try {
            File targetFile = new File(filePath);
            targetFile.getParentFile().mkdirs();
            file.transferTo(targetFile);
            System.out.println("New File Will Be Print");

            Services.printPdf(filePath, "Canon");
            return "打印已经完成";
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
